package cn.rwklyd.ecommerce.mapper;

import cn.rwklyd.ecommerce.pojo.Product;
import cn.rwklyd.ecommerce.pojo.Users;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author 键盘书生
 * @date 2024/12/1
 */
@Mapper
public interface ApiMapper {
    // 用户登录验证
    @Select("select * from users where username=#{username} and password=#{password} and role=#{role}")
    Users login(Users users);

    // 注册用户（销售不提供注册）
    @Insert("insert into users(username,password,email,role) values(#{username},#{password},#{email},#{role})")
    int register(Users users);

    // 用户注册查询
    @Select("select * from users where username=#{username}")
    Users queryUser(Users username);

    // 获取商品列表
    @Select("select * from products")
    public List<Product> getProductList();

    // 用户充值
    @Update("update users set balance=balance+#{amount} where id=#{userId}")
    Boolean rechargeQRCode(String userId, double amount);

    // 用户余额查询
    @Select("select balance from users where id=#{userId}")
    public Double pollingBalance(String userId);

    // 搜索商品
    @Select("SELECT * FROM products " +
            "WHERE name LIKE CONCAT('%', #{key}, '%') " +
            "OR sort LIKE CONCAT('%', #{key}, '%')")
    List<Product> search(String key);


}
